﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Filter;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// Adaptive Threshold
    /// </summary>
    public partial class FormAdaptiveThreshold : FormFilter
    {
        protected AdaptiveThresholding adaptiveThresholding = new AdaptiveThresholding();
        /// <summary>
        /// Constructor
        /// </summary>
        public FormAdaptiveThreshold()
        {
            InitializeComponent();
            this.Shown += new EventHandler(FormAdaptiveThreshold_Shown);
            pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;

            InitializeThresholdModeComboBox();
        }

        private void InitializeThresholdModeComboBox()
        {
            foreach (THRESHOLDING_MODE MODE in Enum.GetValues(typeof(THRESHOLDING_MODE)))
            {
                this.cobMode.Items.Add(MODE);
            }
            cobMode.DropDownStyle = ComboBoxStyle.DropDownList;
            cobMode.SelectedIndex = 0;
        }

        /// <summary>
        /// Constructor provides opportunity to give mode
        /// </summary>
        /// <param name="_thresholdingMode"></param>
        public FormAdaptiveThreshold(THRESHOLDING_MODE _thresholdingMode)
            : this()
        {
            cobMode.SelectedItem = _thresholdingMode;
        }

        /// <summary>
        /// Shown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FormAdaptiveThreshold_Shown(object sender, EventArgs e)
        {
            m_ImageOriginal = (Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage.Clone();
            pictureBox1.BackgroundImage = m_ImageOriginal;
            pictureBox1.Invalidate();
        }

        /// <summary>
        /// Adaptive Threshold
        /// </summary>
        private void AdaptiveThreshold()
        {
            adaptiveThresholding.WindowSize = (int)nudWindowSize.Value;
            adaptiveThresholding.ThresholdingMode = (THRESHOLDING_MODE)cobMode.SelectedItem;
            adaptiveThresholding.Constant = (int)nudMeanOffset.Value;

            Bitmap bitmap = (Bitmap)m_ImageOriginal.Clone();
            adaptiveThresholding.Execute(bitmap).Dispose();
            pictureBox1.BackgroundImage = bitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbOK_Click(object sender, EventArgs e)
        {
            AdaptiveThreshold();
            MainFormSingleton.ActivePictureBox.BackgroundImage = pictureBox1.BackgroundImage;
            MainFormSingleton.ActivePictureBox.Invalidate();
        }
    }
}
